﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Read.API.Utils;

namespace MS.BasicData.Read.API.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SecureController : ControllerBase
    {
        private readonly UserDataDbconterxt _dbContext;

        private readonly SmsClientFactory _smsClientFactory;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="smsClientFactory"></param>
        public SecureController(UserDataDbconterxt dbContext, SmsClientFactory smsClientFactory)
        {
            _dbContext = dbContext;
            _smsClientFactory = smsClientFactory;
        }


        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSecureData()
        {
            return Ok("This is a secure data");
        }
        /// <summary>
        /// 注册（审计字段）
        /// </summary>
        /// <param name="info"></param>
        [HttpPost]
        public void Add(UserInfo info)
        {
            var user = new UserInfo
            {
                UserName = info.UserName,
                UserNick = info.UserNick,
                Password = info.Password,
            };
            _dbContext.UserInfos.Add(user);
            _dbContext.SaveChanges();

        }
        /// <summary>
        /// 更新（审计字段）
        /// </summary>
        /// <param name="id"></param>
        [HttpPost("{id}")]
        public void Update(int id)
        {
            var user = _dbContext.UserInfos.Find(id);
            user.UserName = "Test Updated";
            _dbContext.SaveChanges();
        }
        /// <summary>
        /// 删除（审计字段）
        /// </summary>
        /// <param name="id"></param>
        [HttpPost("{id}")]
        public void Delete(int id)
        {
            var user = _dbContext.UserInfos.Find(id);
            _dbContext.UserInfos.Remove(user);
            _dbContext.SaveChanges();
        }

        /// <summary>
        /// 发送短信
        /// </summary>  
        [HttpGet("sendSms")]
        public void SendSms()
        {
            _smsClientFactory.SendSms("17532528589", "{\"code\":\"1234\"}", "阿里云短信测试", "SMS_154950909");
        }
    }
}
